/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <string>
#include <iostream>

#include "FormatType.h"
#include "ProtocolType.h"
#include "Exception.h"
#include "HttpRequest.h"

#define TRACE(expr) std::cout << #expr << ": " << (expr) << std::endl

void testFormatType() {
  using namespace aliyun::http;
  FormatType format;

  std::string testNames[] = { "application/xml", "application/json",
      "application/octet-stream", "application/unkow-type" };

  enum FooEnum { UNKNOW };
  TRACE(sizeof(FooEnum));

  // construct from string, cover mapAcceptToFormat & mapFormatToAccept
  for (int i = 0; i < sizeof(testNames) / sizeof(testNames[0]); i++) {
    format = FormatType::mapAcceptToFormat(testNames[i]);
    TRACE(testNames[i]);
    TRACE(format);
    TRACE(FormatType::mapFormatToAccept(format));
  }

  format = FormatType::XML;
  TRACE(FormatType::XML);
  TRACE(FormatType::JSON);
  TRACE(FormatType::RAW);

  TRACE(FormatType::mapFormatToAccept(FormatType::XML));
  TRACE(FormatType::mapFormatToAccept(FormatType::JSON));
  TRACE(FormatType::mapFormatToAccept(FormatType::RAW));

  std::cout << std::endl;
}

void testProtoclType() {
  using namespace aliyun::http;
  using namespace aliyun;

  ProtocolType protocol;

  std::string testNames[] = { "http", "https", "unknow" };

  for (int i = 0; i < sizeof(testNames) / sizeof(testNames[0]); i++) {
    try {
      protocol = ProtocolType(testNames[i]);
    } catch (Exception& e) {
      std::cout << "## Exception ##\n what: " << e.what() << std::endl;
      std::cout << "stack:\n" << e.stackTrace() << std::endl;
    }
    TRACE(testNames[i]);
    TRACE(protocol);
    TRACE(static_cast<std::string>(protocol));
  }

  protocol = ProtocolType::HTTP;
  TRACE(ProtocolType::HTTP);
  TRACE(ProtocolType::HTTPS);

  TRACE(static_cast<std::string>(ProtocolType(ProtocolType::HTTP)));
  TRACE(static_cast<std::string>(ProtocolType(ProtocolType::HTTPS)));

  std::cout << std::endl;
}

// g++ HttpTest.cc -I .. -I ../http/ -I ../utils/ -I ../../external/curl-7.45.0/include/
int main(int argc, char* argv[]) {
  testFormatType();

  testProtoclType();

  return 0;
}

